# SSAS - Simple Smart Automotive Software
# Copyright (C) 2023 Parai Wang <parai@foxmail.com>

import pprint
import os
import json
from .helper import *

__all__ = ['Gen']


def Gen_CDD_RF_cfg_INCLUDES(cfg, outfile):
    outfile.write(
        '/* ================================ [ INCLUDES  ] ============================================== */\n')
    outfile.write('#include "CDD_RF_Types.h"\n')
    outfile.write('#include "CDD_RF_Internal.h"\n')
    outfile.write('#include "DSV_Macro.h"\n')

def Gen_CDD_RF_cfg_MACROS(cfg, outfile):
    outfile.write(
        '/* ================================ [ MACROS    ] ============================================== */\n')

    for Id, profile in enumerate(cfg['profile']):
        outfile.write("#define      %s_START_FREQ_SRR   %f \n" % (profile["name"], profile["START_FREQ_SRR"]))
        outfile.write("#define      %s_START_FREQ_MRR   %f \n" % (profile["name"], profile["START_FREQ_MRR"]))
        outfile.write("#define      %s_SLOPE_FREQ_SRR   %f \n" % (profile["name"], profile["SLOPE_FREQ_SRR"]))
        outfile.write("#define      %s_SLOPE_FREQ_MRR   %f \n" % (profile["name"], profile["SLOPE_FREQ_MRR"]))
        outfile.write("\n\n")


    for Id, definition in enumerate(cfg['DEFINITION']):
        outfile.write("#define      %s    %s \n" % (definition["name"], definition["Value"]))


def Gen_CDD_RF_cfg_TYPES(cfg, outfile):
    outfile.write(
        '/* ================================ [ TYPES     ] ============================================== */\n')
    pass

def Gen_CDD_RF_cfg_DECLARES(cfg, outfile):
    outfile.write(
        '/* ================================ [ DECLARES     ] ============================================== */\n')
    pass

def Gen_CDD_RF_cfg_DATAS(cfg, outfile):
    outfile.write(
        '/* ================================ [ DATAS     ] ============================================== */\n')
    pass

def Gen_CDD_RF_cfg_LOCALS(cfg, outfile):
    outfile.write(
        '/* ================================ [ LOCALS     ] ============================================== */\n')
    pass

def Gen_CDD_RF_cfg_FUNCTIONS(cfg, outfile):
    outfile.write(
        '/* ================================ [ FUNCTIONS     ] ============================================== */\n')
    pass

def Gen_CDD_RF_cfg_DUMP(cfg, outfile):
    outContent = r"""
    
typedef enum
{
    SIGNAL_MRR_SUBFRAME_MRR1_ID = 0x00U
}SIGNAL_MRR_SUB_FRAME_T ;


typedef enum
{
    SIGNAL_MRR_SUBFRAME_MRR2_ID = 0x00U
}SIGNAL_MRR2_SUB_FRAME_T ;


typedef enum
{
    SIGNAL_MRR_SUBFRAME_MRR3_ID = 0x00U
}SIGNAL_MRR3_SUB_FRAME_T ;

typedef enum
{
    SIGNAL_SRR_SUBFRAME_SRR_ID = 0x00U
}SIGNAL_SRR_SUB_FRAME_T ;




typedef enum 
{

    MULTI_SUBFRAME_SRR_ID  = 0x00U ,
    MULTI_SUBFRAME_MRR_ID  = 0x01U ,
    MULTI_SUBFRAME_LRR_ID  = 0x02u ,

}MULTI_SUN_FRAME_T ;






typedef enum
{
    PROFILE_CFG_ID_SRR  , 
    PROFILE_CFG_ID_MRR1  ,
    PROFILE_CFG_ID_MRR2  ,
    PROFILE_CFG_ID_MRR3  ,

    
#if ( SRR_MRR_LRR_CHIRP_ENABLE == STD_ON )
    PROFILE_CFG_ID_LRR1 ,
    PROFILE_CFG_ID_LRR2 ,
    PROFILE_CFG_ID_LRR3 ,
#endif 

    PROFILE_CFG_NUM     ,       

} MMWAVE_PROFILE_CFG_ENUM_T ;


typedef enum
{

    CHIRP_CFG_ID_SRR_A = 0x0 ,
    CHIRP_CFG_ID_SRR_B ,
    CHIRP_CFG_ID_MRR_1 ,
#if ( SRR_MRR_LRR_CHIRP_ENABLE == STD_ON )
    CHIRP_CFG_ID_LRR_1 ,
#endif
    // CHIRP_CFG_ID_MRR_2 ,
    // CHIRP_CFG_ID_MRR_3 ,
    CHIRP_CFG_NUM  ,

} MMWAVE_CHIRP_CFG_ENUM_T ;






typedef enum
{

    MUX_ADV_FRAME_SRR_MRR1 , 
    MUX_ADV_FRAME_SRR_MRR2 ,
    MUX_ADV_FRAME_SRR_MRR3 ,
    MUX_ADV_FRAME_NUM, 

} MMWAVE_MUX_ADV_FRAME_CFG_ENUM_T ;

typedef enum
{

    FRAME_CFG_ID_SRR       ,
    FRAME_CFG_ID_MRR_1     ,
    FRAME_CFG_ID_MRR_2     ,
    FRAME_CFG_ID_MRR_3     ,
    FRAME_CFG_ID_NUM  ,
} MMWAVE_FRMAE_CFG_ENUM_T ;



typedef enum
{
    // SRR_DYNC_CHIRP_ID ,
    MRR_DYNC_CHIRP_ID ,
    LRR_DYNC_CHIRP_ID ,
    MRR_DYNC_CHIRP_ID_NUM ,
} MMWAVE_DYNC_CHIRP_CFG_ENUM_T ;

typedef enum
{
    SRR_DYNC_PH_SHIFT_ID,
    MRR_DYNC_PH_SHIFT_ID,
    DYNC_PH_SHIFT_ID_NUM ,
    
} MMWAVE_DYNC_PHASE_SHIFT_CFG_ENUM_T ;


/**********************************************************************************************************************
 *  GLOBAL VARIABLES
 *********************************************************************************************************************/

/**********************************************************************************************************************
 *  GLOBAL CONST VARIABLES
 *********************************************************************************************************************/
extern  rfCdd_ConfigType   rfCdd_Config ;

#if (RF_CALIBRATION_MONITOR == STD_ON)
/********************************************************************************
 **
 * @brief   Analog monitoring configuration data
 **
 ********************************************************************************/
extern const rlMonAnaEnables_t GstrMonAnaEnableCfg;
#endif

    
    """


    outfile.write(outContent)
    pass


def Gen_CDD_RF_Cfg_H(cfg, dir):
    H = open('%s/CDD_RF_Cfg.h' % dir, 'w')
    GenHeader(H)
    H.write('#ifndef __CDD_RF_Cfg_H__\n')
    H.write('#define __CDD_RF_Cfg_H__\n')

    Gen_CDD_RF_cfg_INCLUDES(cfg, H)

    Gen_CDD_RF_cfg_MACROS(cfg, H)

    Gen_CDD_RF_cfg_DUMP(cfg, H)
    #
    # Gen_CDD_RF_cfg_TYPES(cfg, H)
    #
    # Gen_CDD_RF_cfg_DECLARES(cfg, H)
    #
    # Gen_CDD_RF_cfg_DATAS(cfg, H)
    #
    # Gen_CDD_RF_cfg_LOCALS(cfg, H)
    #
    # Gen_CDD_RF_cfg_FUNCTIONS(cfg, H)

    H.write('\n\n\n#endif /* __CDD_RF_Cfg_H__ */\n')
    H.close()






def Gen_CDD_RF_Cfg_C(cfg, dir):
    C = open('%s/CDD_RF_Cfg.c' % dir, 'w')
    GenHeader(C)
    C.write(
        '/* ================================ [ INCLUDES  ] ============================================== */\n')
    C.write('#include "Std_Trace.h"\n')
    C.write('#ifdef USE_SHELL\n')
    C.write('#include "shell.h"\n')
    C.write('#endif\n')
    C.write(
        '/* ================================ [ MACROS    ] ============================================== */\n')
    C.write(
        '/* ================================ [ TYPES     ] ============================================== */\n')
    C.write(
        '/* ================================ [ DECLARES  ] ============================================== */\n')
    C.write(
        '/* ================================ [ DATAS     ] ============================================== */\n')
    # C.write('RB_DECLARE(TraceArea%s, Std_TraceEventType, %s);\n' % (area, cfg.get('size', 1024)))
    # C.write('const Std_TraceAreaType Std_TraceArea_%s = {\n' % (area.upper()))
    # C.write('  &rb_TraceArea%s,\n' % (area))
    C.write('};\n\n')
    C.write(
        '/* ================================ [ LOCALS    ] ============================================== */\n')
    C.write(
        '/* ================================ [ FUNCTIONS ] ============================================== */\n')
    C.write('#ifdef USE_SHELL\n')
    # C.write('static int Shell_Trace%s(int argc, const char *argv[]) {\n' % (area))
    # C.write('  Std_TraceDump(&Std_TraceArea_%s);\n' % (area.upper()))
    # C.write('  return 0;\n')
    # C.write('}\n')
    # C.write('SHELL_REGISTER(trace_%s, "trace_%s\\n", Shell_Trace%s)\n' %
    #         (area.lower(), area.lower(), area))
    C.write('#endif\n')
    C.close()

def extract(cfg, dir):
    cfg_ = {}
    for k, v in cfg.items():
        if k not in ['durations']:
            cfg_[k] = v

    with open('%s/CDD_RF.json' % (dir), 'w') as f:
        json.dump(cfg_, f, indent=2)
    return cfg_


def Gen(cfg):
    dir = os.path.join(os.path.dirname(cfg), 'GEN')
    os.makedirs(dir, exist_ok=True)
    with open(cfg) as f:
        cfg = json.load(f)
    cfg_ = extract(cfg, dir)
    Gen_CDD_RF_Cfg_H(cfg_, dir)
    Gen_CDD_RF_Cfg_C(cfg_, dir)
